using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Oarw.General;

namespace ScreenMonkey.Plugin.Clips.Song
{
    public partial class SongLiveMonitor : UserControl
    {
        private List<Label> verseList = new List<Label>();

        private List<ToolStripButton> verseOrderButtons = new List<ToolStripButton>();
        private ToolStripButton highlightVerseButton = null;

        private EventHandler VerseClickCallback;

        public event EventHandler VerseSelected;

        public SongLiveMonitor()
        {
            InitializeComponent();

            VerseClickCallback = new EventHandler(verse_Click);
        }

        private SongClip clip;

        public SongClip Clip
        {
            get { return clip; }
            set
            {
                clip = value;
                if (clip != null)
                {
                    clip.VerseChanged += new EventHandler(clip_VerseChanged);
                    clip.VerseSectionChanged += new EventHandler(clip_VerseSectionChanged);
                }
            }
        }

        void clip_VerseSectionChanged(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new EventHandler(clip_VerseSectionChanged), sender, e);
                return;
            }

            display.SectionIndex = clip.SubSlideIndex;


            if (highlightVerseButton != null && clip.SubSlideCount(clip.VerseIndex)>1)
            {
                if (clip.SubSlideIndex == clip.SubSlideCount(clip.VerseIndex) - 1)
                {
                    highlightVerseButton.Text = highlightVerseButton.Text.Replace("+", "");
                }
                else
                {
                    if (!highlightVerseButton.Text.Contains("+"))
                    {
                        highlightVerseButton.Text += "+";
                    }
                }
            }
        }

        void clip_VerseChanged(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new EventHandler(clip_VerseChanged), sender, e);
                return;
            }

            UpdateDisplay();
            HighlightedVerse = clip.SlideIndex;
        }

        private int highlightedVerse = -1;

        public int HighlightedVerse
        {
            get
            {
                return highlightedVerse;
            }
            set
            {
                if (highlightedVerse >= 0 && highlightedVerse < verseList.Count) verseList[highlightedVerse].BackColor = Color.White;

                highlightedVerse = value;

                if (highlightedVerse >= 0 && highlightedVerse < verseList.Count)
                {
                    verseList[highlightedVerse].BackColor = Color.Yellow;
                    versePnl.ScrollControlIntoView(verseList[highlightedVerse]);
                }

                if (highlightedVerse >= 0 && highlightedVerse < verseOrderButtons.Count)
                    HighlightVerseButton(verseOrderButtons[highlightedVerse]);
            }
        }

        private void HighlightVerseButton(ToolStripButton button)
        {
            if (highlightVerseButton != null)
            {
                highlightVerseButton.Checked = false;
                highlightVerseButton.Text = highlightVerseButton.Text.Replace("+","");
            }

            highlightVerseButton = button;
            highlightVerseButton.Checked = true;

            if (clip.SubSlideCount(clip.VerseIndex) > 1)
            {
                highlightVerseButton.Text += "+";
            }
        }


        public void LoadClip()
        {
            ExpandableLabel newLabel;

            //Load Verses
            ClearVerses();

            if (Clip is SongClip)
            {
                //----- Load The Verses Text -----
                int index = 0;
                foreach (VerseLyrics verse in Clip.Lyrics.Sequence)
                {
                    newLabel = new ExpandableLabel();
                    newLabel.BackColor = Color.White;
                    newLabel.VerticalAutoSize = true;
                    newLabel.Width = versePnl.Width;
                    newLabel.Padding = new Padding(0,3,0,3);
                    newLabel.Text = verse.ToString(VerseLyrics.VerseContent.LyricsAndLabel);
                    newLabel.AutoSize = false;
                    newLabel.Dock = DockStyle.Top;
                    newLabel.TextAlign = ContentAlignment.MiddleLeft;
                    
                    
                    newLabel.Click += VerseClickCallback;
                    newLabel.Tag = new KeyValuePair<int,VerseLyrics>(index,verse);
                    
                    versePnl.Controls.Add(newLabel);
                    newLabel.BringToFront();

                    verseList.Add(newLabel);

                    index++;
                }

                //----- Load the Verse Order -----
                ToolStripButton verseButton;

                verseOrderButtons.Clear();
                verseOrder.Items.Clear();

                foreach (VerseLyrics verse in Clip.Lyrics.Sequence)
                {
                    verseButton = new ToolStripButton();
                    verseButton.DisplayStyle = ToolStripItemDisplayStyle.Text;
                    verseButton.Text = verse.Label.QuickName;
                    verseButton.ForeColor = Color.Yellow;
                    verseButton.BackColor = verseOrder.BackColor;
                    verseButton.Font = new Font(verseButton.Font, FontStyle.Bold);

                    verseButton.Click += new EventHandler(verseButton_Click);

                    verseOrder.Items.Add(verseButton);
                    verseOrderButtons.Add(verseButton);
                }

                //Load Display
                UpdateDisplay();

                HighlightedVerse = clip.SlideIndex;
            }
        }

        void verseButton_Click(object sender, EventArgs e)
        {
            ToolStripButton item = sender as ToolStripButton;

            if (item != null)
            {
                int index = verseOrderButtons.IndexOf(item);
                if (index >= 0)
                {
                    if (index == clip.VerseIndex && clip.SubSlideIndex < clip.SubSlideCount(index)-1)
                        clip.NextSlide();
                    else
                        clip.VerseIndex = index;
                }
            }
        }


        void verse_Click(object sender, EventArgs e)
        {
            KeyValuePair<int,VerseLyrics> verseReference = (KeyValuePair<int,VerseLyrics>)((Label)sender).Tag;
            if(clip is SongClip)
            {
                clip.GotoSlide(verseReference.Key);
                OnVerseSelected();
            }
        }

        protected void OnVerseSelected()
        {
            if (VerseSelected != null) VerseSelected(this, EventArgs.Empty);
        }

        private void UpdateDisplay()
        {
            display.DisplayStyle = clip.DisplayStyle;

            if (clip.CurrentLyrics is VerseLyrics)
            {
                display.Copyright = clip.Lyrics.Copyright;
                display.Words = clip.CurrentLyrics.ToString(VerseLyrics.VerseContent.LyricsOnly);
                display.SectionIndex = 0;
            }
        }

        private void ClearVerses()
        {
            foreach (Label verse in verseList)
            {
                versePnl.Controls.Remove(verse);
                verse.Dispose();
            }

            verseList.Clear();
        }

        private float aspectRatio=0;

        public float AspectRatio
        {
            get { return aspectRatio; }
            set { aspectRatio = value;
            ResizeWordsPreview();
            }
        }
	

        private void previewPnl_Resize(object sender, EventArgs e)
        {
            ResizeWordsPreview();
        }

        private void ResizeWordsPreview()
        {
            int height,width, x , y;

            if (AspectRatio == 0)
            {
                display.Dock = DockStyle.Fill;
            }
            else
            {
                if(display.Dock!= DockStyle.None) display.Dock = DockStyle.None;


                width = previewPnl.Width;
                height = (int)((float)width / AspectRatio);
                x = 0;
                y = (previewPnl.Height - height) / 2;

                if(height > previewPnl.Height)
                {
                    height = previewPnl.Height;
                    width = (int)((float) height * AspectRatio);
                    x = (previewPnl.Width - width)/2;
                    y = 0;
                }

                display.SuspendLayout();
                display.Location = new Point(x, y);
                display.Size = new Size(width,height);
                display.ResumeLayout();                
            }
        }

        private void SongLiveMonitor_Load(object sender, EventArgs e)
        {
            verseOrder.Renderer = new VerseOrderToolstripRenderer();
            display.RefreshWordLayout();
        }

    }
}